//
// 作者：AKA_GGbond
// 时间：2025/7/16
// 邮箱：evain_wl@163.com
//

#include "W_LoginButton.h"
#include <QPropertyAnimation>
#include <QPainter>
#include <QPainterPath>
#include <QMouseEvent>

W_LoginButton::W_LoginButton(QWidget *parent)
    : QPushButton(parent)
{
    this->resize(392, 57);
    this->setCursor(Qt::PointingHandCursor);
    setMouseTracking(true);
    startAnimation();
}

W_LoginButton::~W_LoginButton()
{

}

int W_LoginButton::color_opacity() const
{
    return m_color_opacity;
}

void W_LoginButton::setColor_opacity(int newColor_opacity)
{
    m_color_opacity = newColor_opacity;
    update();
}

QString W_LoginButton::center_text() const
{
    return m_center_text;
}

void W_LoginButton::setCenter_text(const QString &newCenter_text)
{
    m_center_text = newCenter_text;
    update();
}

void W_LoginButton::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setViewport(0, 0, width(), height());
    painter.setWindow(0, 0, width(), height());

    QPainterPath path;
    path.addRoundedRect(0, 0, width(), height(), 10, 10);
    painter.setClipPath(path);
    painter.setPen(Qt::NoPen);

    QBrush Brush(m_bgColor);
    painter.setBrush(Brush);
    painter.drawRect(0, 0, width(), height());

    drawText();
}

bool W_LoginButton::event(QEvent* e)
{
    if (e->type() == QEvent::Enter)
    {
        m_animation->setDirection(QPropertyAnimation::Forward);
        m_animation->start();
//        qDebug() << "鼠标进入";
    }
    else if (e->type() == QEvent::Leave)
    {
        m_animation->setDirection(QPropertyAnimation::Backward);
        m_animation->start();
//        qDebug() << "鼠标离开";
        update();
    }

    return QPushButton::event(e);
}

void W_LoginButton::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        emit execute_animation_signal(AnimationState::Execute);
        emit clicked(true);
    }
    QWidget::mousePressEvent(event);
}

void W_LoginButton::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) emit execute_animation_signal(AnimationState::Restore);
    QWidget::mouseReleaseEvent(event);
}

void W_LoginButton::drawText()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);

    QRect rect1(0, 0, 0, 0);
    QFont font1;
    font1.setPointSize(13);
    font1.setBold(true);
    font1.setWordSpacing(1);
    painter.setFont(font1);

    QColor semiTransparent(255, 255, 255, 255);
    painter.setPen(semiTransparent);

    QRect actualRect = painter.boundingRect(rect1, Qt::AlignCenter, m_center_text);
    rect1.setHeight(actualRect.height());
    rect1.setWidth(actualRect.width());
    rect1.moveCenter(QPoint(width() / 2, height() / 2));
    painter.drawText(rect1, m_center_text);
}

void W_LoginButton::startAnimation()
{
    m_animation = new QPropertyAnimation(this, "color_opacity");
    m_animation->setDuration(200);
    m_animation->setStartValue(this->color_opacity());
    m_animation->setEndValue(188);
    m_animation->setEasingCurve(QEasingCurve::Linear);
}

void W_LoginButton::setbgColor(QColor bgColor)
{
    m_bgColor = QColor(bgColor.red(),bgColor.green(),bgColor.blue(),m_color_opacity);
    update();
}

QColor W_LoginButton::bgColor() const {
    return m_bgColor;
}
